# import os
# import sys
# import platform
# import random
# import re
# import time
# import shutil
# import chardet

# #旧的工程目录名称
# g_old_project_Path = "/Users/yanlinyan/Desktop/yly/-1-OC_laji/python_yly/SDK_HuanPi/Rank/SDK"
# g_old_project_name="UltaPDP"
# user_input ='ProMatataPro'

# def get_dirs_files_list(rootdir,pathlist,dirlist):
#     list = os.listdir(rootdir)
#     for i in range(0, len(list)):
#         path = os.path.join(rootdir, list[i])
#         if os.path.isfile(path):
#             ext=os.path.splitext(path)[1]
#             if(1|(".cpp" == ext)|(".hpp" == ext)|(".h" == ext)|(".c" == ext)):
#                 #print (path)
#                 pathlist.append(path)
#         else:
#             #过滤不要的文件夹
#             if((".\\_xxx_filter_xxx" == path)):
#                 continue;
#             #print(path)
#             dirlist.append(path)
#             get_dirs_files_list(path,pathlist,dirlist)
# def new_project(prj_name):
#     new_prj_main_dir=".\\"+prj_name;
#     src_path_list=[];
#     src_dir_list=[];
#     get_dirs_files_list(g_old_project_Path+".\\"+g_old_project_name,src_path_list,src_dir_list)
#     if(os.path.exists(new_prj_main_dir)):
#         shutil.rmtree(new_prj_main_dir)
#     build_new_directory_structure(prj_name,src_dir_list);
#     filespath_out=build_new_files_structure(prj_name,src_path_list);
#     reset_prjname_in_files(prj_name,filespath_out);
#     print("项目创建完毕:"+prj_name)
# def build_new_directory_structure(prj_name,src_dir_list):
#     for dirpath in src_dir_list:
#         dirpath=dirpath.replace(g_old_project_name,prj_name);
#         print("正在创建目录:"+dirpath)
#         os.makedirs(dirpath)
#     return;
# def build_new_files_structure(prj_name,src_path_list):
#     filespath_out=[]
#     for filespath in src_path_list:
#         newpath=filespath.replace(g_old_project_name,prj_name);
#         shutil.copyfile(filespath,newpath)
#         print("正在创建文件:"+newpath)
#         filespath_out.append(newpath);
#     return filespath_out;
# def check_codes(rpath):
#     f = open(rpath,'rb')
#     data = f.read()
#     rt=chardet.detect(data)
#     return rt['encoding'];
# def replace_file_string(path,search_string,to_string):
#     #print("正在分析编码:"+path)
#     ext=os.path.splitext(path)[1]
#     #过滤无需编码分析的文件
#     if((""==ext)|(".jpg" in ext)|(".png" in ext)|(".ttf" in ext)|(".Bin" in ext)|(".bin" in ext)):
#         return;
#     sig=check_codes(path)
#     #print("编码:"+sig,"ex:",ext)
#     if((sig==None)|(sig=="None")|(sig=="Windows-1254")|(sig=="Windows-1252")):
#         return;
#     print("正在处理文件:"+path+" encoding:"+sig)
#     f=open(path,'r',encoding=sig)
#     alllines=f.readlines()
#     f.close()
#     f=open(path,'w+',encoding=sig)
#     for eachline in alllines: 
#         a=eachline.replace(search_string,to_string)
#         f.writelines(a)
#     f.close()
#     #print("正在处理文件:"+str(len(alllines)))
# def reset_prjname_in_files(prj_name,filespath_out):
#     for ipath in filespath_out:
#         replace_file_string(ipath,g_old_project_name,prj_name)
 
# #此.py文件应与旧的工程目录同级下执行
# if __name__ == '__main__':
#     #user_input = input("project name:")
#     #新的工程目录名称
#     if(user_input==g_old_project_name):
#         print("工程名不允许为:"+g_old_project_name)
#     else:
#         new_project(user_input);

import os
import plistlib
import re
 
def modify_ios_project_name(project_path, new_name):
    project_name_pattern = re.compile(r'PRODUCT_NAME = (.+);')
    
    # 搜索 xcscheme 文件
    for dirpath, dirnames, files in os.walk(project_path):
        for file in files:
            if file.endswith('.xcscheme'):
                scheme_path = os.path.join(dirpath, file)
                with open(scheme_path, 'rb') as f:
                    scheme = plistlib.load(f)
                scheme['Scheme']['name'] = new_name
                with open(scheme_path, 'wb') as f:
                    plistlib.dump(scheme, f)
    
    # 修改 project.pbxproj 文件中的 PRODUCT_NAME
    pbxproj_path = os.path.join(project_path, 'project.pbxproj')
    with open(pbxproj_path, 'r') as f:
        lines = f.readlines()
    
    modified_lines = []
    for line in lines:
        match = re.search(project_name_pattern, line)
        if match:
            modified_lines.append(f'    PRODUCT_NAME = "{new_name}";\n')
        else:
            modified_lines.append(line)
    
    with open(pbxproj_path, 'w') as f:
        f.writelines(modified_lines)
 
# 使用示例
project_path = '/Users/yanlinyan/Desktop/yly/20240410_XXX/ProMatataPro'
modify_ios_project_name(project_path, 'ProMatataPro')
